﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Eflong.MonitoringLib.MonitoringUnit
{
    public class CoalMineUnitManager
    {
        private static CoalMineUnitManager _instance = null;

        public static CoalMineUnitManager Instance( )
        {
            if ( _instance == null )
            {
                _instance = new CoalMineUnitManager( );
            }
            return _instance;
        }

        private CoalMineUnitManager( )
        {
            dic_CoalMineUnit = new Dictionary<long , BaseCoalMineUnit>( );
            IsRealDataReady = false;
        }

        ~CoalMineUnitManager( )
        {
            Dispose( false );
        }

        public void Dispose( )
        {
            Dispose( true );

            System.GC.SuppressFinalize( this );

            // 上面一行代码作用是防止"垃圾回收器"调用这个类中的方法
            // " ~ResourceHolder() "
            // 为什么要防止呢? 因为如果用户记得调用Dispose()方法,那么
            // "垃圾回收器"就没有必要"多此一举"地再去释放一遍"非托管资源"了
            // 如果用户不记得调用呢,就让"垃圾回收器"帮我们去"多此一举"吧 ^_^
            // 你看不懂我上面说的不要紧,下面我还有更详细的解释呢!
        }

        protected virtual void Dispose( bool disposing )
        {
            if ( disposing )
            {
                // 这里是清理"托管资源"的用户代码段
                DeleteAllCoalMineUnits( );
            }

            // 这里是清理"非托管资源"的用户代码段
        }

        private object syncObject = new object( );

        private Dictionary<long , BaseCoalMineUnit> dic_CoalMineUnit;

        public bool IsRealDataReady { get; set; }

        /// <summary>
        /// 根据矿井名称获取矿信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BaseCoalMineUnit GetCoalMineUnitByName( string name )
        {
            BaseCoalMineUnit coalMineUint = dic_CoalMineUnit.Values.First( u => u.m_strName == name );
            return coalMineUint;
        }

        /// <summary>
        /// 加入一个联网矿信息
        /// </summary>
        /// <param name="nCoalMineId">矿编号</param>
        /// <param name="strName">矿名称</param>
        /// <param name="strIPAddr">矿IP地址</param>
        public void AddCoalMineUnit( UInt32 nCoalMineId , string strName , string strIPAddr )
        {
            BaseCoalMineUnit coalMineUint = new BaseCoalMineUnit( );
            coalMineUint.m_nId = nCoalMineId;
            coalMineUint.m_strName = strName;
            coalMineUint.m_strIPAddr = strIPAddr;
            lock ( syncObject )
            {
                dic_CoalMineUnit.Add( nCoalMineId , coalMineUint );
            }
        }

        public void AddCoalMineUnit( BaseCoalMineUnit item )
        {
            lock ( syncObject )
            {
                if ( !dic_CoalMineUnit.ContainsKey( item.m_nId ) )
                {
                    dic_CoalMineUnit.Add( item.m_nId , item );
                }
                else
                {
                    dic_CoalMineUnit[ item.m_nId ] = item;
                }
            }
        }

        public BaseCoalMineUnit GetCoalMineUnit( long nId )
        {
            BaseCoalMineUnit coalMineUint = null;
            if ( dic_CoalMineUnit.ContainsKey( nId ) )
            {
                coalMineUint = dic_CoalMineUnit[ nId ];
            }
            return coalMineUint;
        }

        /// <summary>
        /// 获取全部的联网矿信息
        /// </summary>
        /// <returns></returns>
        public BaseCoalMineUnit[ ] GetAllCoalMineUnits( )
        {
            BaseCoalMineUnit[ ] units = dic_CoalMineUnit.Values.ToArray( );
            return units;
        }

        public BaseMonitoringUnit GetMonitoringUnit( long nId , string sId )
        {
            BaseMonitoringUnit monitoringUint = null;
            BaseCoalMineUnit coalMineUnit = GetCoalMineUnit( nId );
            if ( coalMineUnit != null )
            {
                monitoringUint = coalMineUnit.GetMonitoringUnit( sId );
            }
            return monitoringUint;
        }

        public int GetCoalMineUnitCount( )
        {
            return dic_CoalMineUnit.Count;
        }

        /// <summary>
        /// 修改联网矿信息
        /// </summary>
        /// <param name="nCoalMineId">矿编号</param>
        /// <param name="strName">矿名称</param>
        /// <param name="strIPAddr">矿IP地址</param>
        public void ModifyCoalMineUnit( long nCoalMineId , string strName , string strIPAddr , bool bConnected )
        {
            if ( dic_CoalMineUnit.ContainsKey( nCoalMineId ) )
            {
                lock ( syncObject )
                {
                    BaseCoalMineUnit coalMineUint = dic_CoalMineUnit[ nCoalMineId ];
                    coalMineUint.m_strName = strName;
                    coalMineUint.m_strIPAddr = strIPAddr;
                    coalMineUint.m_bConnected = bConnected;
                }
            }
        }

        public void DeleteCoalMineUnit( long nCoalMineId )
        {
            lock ( syncObject )
            {
                if ( dic_CoalMineUnit.ContainsKey( nCoalMineId ) )
                {
                    BaseCoalMineUnit coalMineUnit = dic_CoalMineUnit[ nCoalMineId ];
                    coalMineUnit.RemoveAllMonitoringUnits( );
                    dic_CoalMineUnit.Remove( nCoalMineId );
                }
            }
        }

        public void DeleteAllCoalMineUnits( )
        {
            lock ( syncObject )
            {
                foreach ( BaseCoalMineUnit coalMineUnit in dic_CoalMineUnit.Values )
                {
                    coalMineUnit.DeleteAllMonitoringUnits( );
                }
                dic_CoalMineUnit.Clear( );
            }
        }

        /// <summary>
        /// 获取联网矿的测点信息
        /// </summary>
        /// <param name="nId">矿编号</param>
        /// <returns>List </returns>
        public List<BaseMonitoringUnit> GetMonitoringUnit( long nId )
        {
            List<BaseMonitoringUnit> list = null;

            //lock (syncObject)
            {
                if ( dic_CoalMineUnit.ContainsKey( nId ) )
                {
                    list = dic_CoalMineUnit[ nId ].GetMonitoringUnits( ).ToList( );
                }
            }
            return list;
        }
    }
}